<?php

/**
* 新闻公告
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class News  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
	* 新闻公告列表
	* Type:1,公告，2：新闻，3：交易规则
	*/
	public function index($type = 0, $title = 0, $p = 1) {
		//公告类型
		$type = (int)$type < 0 ? 0 : (int)$type;
		if($type){
			$where = " where type = '$type'";
		}else{
			$type = 0;
			$where = " where (type = 1 OR type = 2)";
		}
		
		//标题搜索
		$title = sql_format($title);
		if($title){
			$where .= " AND title like '%$title%'";
		} else {
			$title = 0;
		}
	
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(*) as c from news" . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];

		//分页查询
		$sql = "select *  from news" . $where . " order by id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示 
		$url = "/" . $this->_controller . "/" . $this->_action . "/" . $type  . "/" . $title . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("type", $type);
		$this->assign("title", $title);

		$this->display();
	}
	
	/**
	* 新增新闻公告
	* @param    int  	$id   		id
	* @param    int  	$type   	1：公告，2：新闻
	* @param    char  	$author   	发布人
	* @param    int  	$add_time   添加时间
	* @param    char  	$title   	标题
	* @param    text  	$content   	内容
	* @param    char  	$url   		外链地址
	* @param    tinyint $status   	1：有效，0，无效
	*/
	public function add(){
		if(IS_POST){
			$type = isset($_POST["type"]) ? $_POST["type"] : "0";
			$title = isset($_POST["title"]) ? substr(sql_format($_POST["title"]), 0, 255) : "";
			$content= isset($_POST["content"]) ? $_POST["content"] : "";
			$status = isset($_POST["status"]) ? $_POST["status"] : "0";
			$url = isset($_POST["url"]) ? substr(sql_format($_POST["url"]), 0, 100) : "";
			$add_time =  time();
			$author = $_SESSION["admin"]["username"];
			
			if(! $type){
				ajax_return(0, "请选择类别");
			}
			
			if(! $title){
				ajax_return(0, "请输入标题");
			}

			//校验标题
			$sql = "select id from news where type = $type AND title = '$title'";
			$result = $this->db->query($sql)->row_array();
			$result && ajax_return(0, "标题已经存在");
			
			//插入公告
			$sql = "insert into news (type,author,add_time,title,content,url,status) values ('$type','$author','" . $add_time ."','$title','$content','$url','$status')";
			$this->db->query($sql);
			$res =$this->db->insert_id();
			
			if(! $res){
				ajax_return( 0, "添加失败" );
			} else{
				ajax_return( 1, "添加成功" , "/News/index/" . $type  );
			}
		}
		
		$this->display();
	}
	
	/**
	* 编辑新闻公告
	* @param    int  	$id   		id
	* @param    int  	$type   	1：公告，2：新闻
	* @param    char  	$author   	发布人
	* @param    int  	$add_time   添加时间
	* @param    char  	$title   	标题
	* @param    text  	$content   	内容
	* @param    char  	$url   		外链地址
	* @param    tinyint $status   	1：有效，0，无效
	*/
	public function edit($id = 0){
		if(IS_POST){
			
			if(0 >= $id){
				ajax_return( 0 , "参数有误" );
			}
			$type = isset($_POST["type"]) ? $_POST["type"] : "0";
			$title = isset($_POST["title"]) ? substr(sql_format($_POST["title"]), 0, 255) : "";
			$content= isset($_POST["content"]) ? $_POST["content"]: "";
			$url = isset($_POST["url"]) ? substr(sql_format($_POST["url"]), 0, 100) : "";
			$status = isset($_POST["status"]) ? substr(sql_format($_POST["status"]), 0, 1) : "";
			$add_time =  time();
			$author = $_SESSION["admin"]["username"];

			if(! $type){
				ajax_return(0, "请选择类别");
			}
			if(! $title){
				ajax_return(0, "请输入标题");
			}
			
			$sql = "select id from news where id<>$id   AND type = $type and title = '$title'";
			$result = $this->db->query($sql)->row_array();
			$result && ajax_return(0, "标题已经存在");
			
			$sql = "update news set type = '$type',author = '$author',add_time = '" . $add_time . "',title = '$title',content = '$content',url = '$url',status = '$status' where id = $id";
			$this->db->query($sql);
			
			//返回结果集
			ajax_return( 1, "更新成功" , "/News/index/" . $type );
		} else{
			if(0 >= $id){
				$this->error("参数有误");
			}
			$sql = "select * from news where id = $id";
			$result = $this->db->query($sql)->row_array();
			if(! $result){
				$this->error("记录不存在");
			}
			
			$this->assign("result", $result);
			$this->display();
		}
	}
	
	/**
	* 交易规则
	* @param    text  	$content   	内容
	*/
	public function rule($type = 3){
		if(IS_POST){
			$title =  "";
			$content= isset($_POST["content"]) ? sql_format($_POST["content"]) : "";
			$add_time =  time();
			$author = $_SESSION["admin"]["username"];
			$status=1;
			$url="";

		
			if(! $content){
				ajax_return(0, "请输入内容");
			}
			$sql = "select id from news where type = $type ";
			$result = $this->db->query($sql)->row_array();
			if($result){
				$sql = "update news set author = '$author',add_time = '" . $add_time . "',title = '$title',content = '$content',url = '$url',status = '$status' where type = $type";
			}
			else{
				$sql = "insert into news (type,author,add_time,title,content,url,status) values ('$type','$author','" . $add_time ."','$title','$content','$url','$status')";
			}
			
			$this->db->query($sql);
			
			//返回结果集
			ajax_return( 1, "更新成功" , "/News/rule/" );
		} else{
			
			$sql = "select * from news where type = $type";
			$result = $this->db->query($sql)->row_array();
			$this->assign("result", $result);
			$this->display();
		}
	}
	
	/**
     * 删除
	 * @param	int		$id			管理员id
     * @param	array	$result 	返回结果
     */
	public function delete($id = 0){
		if(IS_POST){
			!$id &&	ajax_return(0, "参数有误");
			$sql = "delete from  news where id in ($id)";
			$this->db->query($sql);
			ajax_return(1, "删除成功");
		}
	}
}
